﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;

using Cisoft.IntegrationSystem.Model;
using Cisoft.IntegrationSystem.BLL;

using Cisoft.Cisoft3G.Model;
using Cisoft.Cisoft3G.BLL;

public partial class Business_Cisoft3G_UserManage3G_UserEdit : WebUI.Page.BasePage
{
    protected int Userid;      //声明一个公用变量，用于存放会员编号


    protected void Page_Load(object sender, EventArgs e)
    {
        #region 权限逻辑
        #endregion

        if (Request.QueryString["UserID"] != null && Request.QueryString["UserID"] != "")
        {
            
            Userid = int.Parse(Request.QueryString["UserID"].ToString());

            if (!IsPostBack)
            {
                showUserInfo(Userid);
            }
        }
        if (!Page.IsPostBack)
        {
            GetRole();
        }
    }

    #region 初始绑定管理员角色 public void GetRole()
    /// <summary>
    /// 初始绑定管理员角色
    /// </summary>
    /// <returns></returns>
    /// 2009.9.3 ryan
    public void GetRole()
    {
        Cisoft.IntegrationSystem.BLL.UserRoleBO rolebo = new Cisoft.IntegrationSystem.BLL.UserRoleBO();
        DataSet dst = rolebo.GetList(" [UR_RoleName] not like '%会员%' ");
        if (dst != null)
        {
            ddlUserGroupParent.DataSource = dst;
            ddlUserGroupParent.DataTextField = "UR_RoleName";
            ddlUserGroupParent.DataValueField = "UR_RoleID";
            ddlUserGroupParent.DataBind();
        }
        else
        {
            ddlUserGroupParent.Enabled = false;
        }
    }
    #endregion

    #region 数据绑定 public void showUserInfo(int nUserid)
    /// <summary>
    /// 数据绑定
    /// </summary>
    /// <param name="nUserid"></param>
    public void showUserInfo(int nUserid)
    {
        Cisoft.IntegrationSystem.Model.UserMO modeluserforeditshow = new Cisoft.IntegrationSystem.Model.UserMO();
        Cisoft.IntegrationSystem.BLL.UserBO blluserforeditshow = new Cisoft.IntegrationSystem.BLL.UserBO();
        modeluserforeditshow = blluserforeditshow.GetUserInfo(nUserid);
        //登录名
        //txtUserNameforregiester.Text = modeluserforeditshow.UserName.ToString();
        //真实姓名
        txtRealName.Text = modeluserforeditshow.UserRealName.ToString();
        ViewState["oldname"] = modeluserforeditshow.UserRealName.ToString();
        ddlUserGroupParent.SelectedValue = modeluserforeditshow.Userrole.ToString();
        //部门
        //ddl_Unit.SelectedValue = modeluserforeditshow.UserGroupID.ToString();

        Cisoft.IntegrationSystem.Model.UserInfoMO modeluserInfoforeditshow = new Cisoft.IntegrationSystem.Model.UserInfoMO();
        Cisoft.IntegrationSystem.BLL.UserInfoBO blluserInfoforeditshow = new Cisoft.IntegrationSystem.BLL.UserInfoBO();
        modeluserInfoforeditshow = blluserInfoforeditshow.GetModel(nUserid);

        if (modeluserInfoforeditshow != null)
        {
            //txtJoblName.Text = modeluserInfoforeditshow.UI_JoblName;
            txtPhone.Text = modeluserInfoforeditshow.UI_Phone;
            hid_mobile.Value = modeluserInfoforeditshow.UI_Phone;
            ViewState["oldmobile"] = modeluserInfoforeditshow.UI_Phone;
        }
    }
    #endregion

    #region 修改事件 protected void ibtnedit_Click(object sender, EventArgs e)
    /// <summary>
    /// 修改事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnedit_Click(object sender, EventArgs e)
    {
        Cisoft.IntegrationSystem.Model.UserMO modeluserforedit = new Cisoft.IntegrationSystem.Model.UserMO();
        Cisoft.IntegrationSystem.BLL.UserBO blluserforedit = new Cisoft.IntegrationSystem.BLL.UserBO();
        modeluserforedit = blluserforedit.GetUserInfo(Userid);
        //设置用户组，默认为0
        modeluserforedit.UserGroupID = 0;
        Cisoft.IntegrationSystem.BLL.UserGroupBO bllusergroupforrole = new Cisoft.IntegrationSystem.BLL.UserGroupBO();
        Cisoft.IntegrationSystem.Model.UserGroupMO modelusergroupforrole = new Cisoft.IntegrationSystem.Model.UserGroupMO();
        //设置角色
        modeluserforedit.Userrole = int.Parse(ddlUserGroupParent.SelectedValue.ToString());
        //真实姓名
        modeluserforedit.UserRealName = txtRealName.Text.ToString();

        string strex = "";
        int ntip = -1;
        try
        {
            //modeluserforedit.UserGroupID = int.Parse(ddl_Unit.SelectedValue);
            ntip = blluserforedit.EditRecord(modeluserforedit);

            //管理员信息
            Cisoft.IntegrationSystem.BLL.UserInfoBO blluserInfoedit = new Cisoft.IntegrationSystem.BLL.UserInfoBO();
            Cisoft.IntegrationSystem.Model.UserInfoMO modeluserInfoedit = new Cisoft.IntegrationSystem.Model.UserInfoMO();
            modeluserInfoedit = blluserInfoedit.GetModel(Userid);
            modeluserInfoedit.UI_Phone = txtPhone.Text.Trim().Replace("'", "").ToString();
            //modeluserInfoedit.UI_JoblName = txtJoblName.Text.Trim().Replace("'", "").ToString();

            blluserInfoedit.Update(modeluserInfoedit);

            //修改联系人信息
            UpdateContact(ViewState["oldname"].ToString(), ViewState["oldmobile"].ToString(), modeluserforedit.UserRealName, modeluserInfoedit.UI_Phone);
        }
        catch (Exception ex)
        {
            strex = "修改异常，请联系管理员，信息如下：" + ex.Message.ToString();
            //PageError(strex, "UserManage.aspx", "返回管理", "");
            btnBack_ts.Visible = false;
            PageError(ModalPopupExtender2, lblTS, btnBack_ts, strex);
        }

        if (ntip > 0)
        {
            //PageRight("修改成功", "UserManage.aspx", "返回管理", "");
            btnBack_ts.Visible = true;
            PageRight(ModalPopupExtender2, lblTS, btnBack_ts, "<h4>修改成功</h4>", "UserManage3G.aspx");
        }
        else
        {
            //PageError("修改失败", "UserManage.aspx", "返回管理", "");
            btnBack_ts.Visible = false;
            PageError(ModalPopupExtender2, lblTS, btnBack_ts, strex);
        }
    }
    #endregion

    #region 全为数字
    public bool isnumber(string s)
    {
        int Flag = 0;
        char[] str = s.ToCharArray();
        for (int i = 0; i < str.Length; i++)
        {
            if (Char.IsNumber(str[i]))
            {
                Flag++;
            }
            else
            {
                Flag = -1;
                break;
            }
        }
        if (Flag > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion

    #region 修改联系人
    private void UpdateContact(string oldName,string oldMobile,string RealName, string Mobile)
    {
        ContactsBO bll = new ContactsBO();
        ContactsMO model = new ContactsMO();

        model = bll.GetModel(" CT_Name='" + oldName + "' and CT_Mobile='"+ oldMobile + "' ") ;
        if (model != null)
        {
            model.CT_Name = RealName;
            model.CT_Mobile = Mobile;
            if (ddlUserGroupParent.SelectedValue.IndexOf("会员") >= 0)
            {
                model.CT_IsUser = 0;
            }
            bll.Update(model);
        }
    }
    #endregion
    
}
